package com.ugoodtech.mdcc.web.controller;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.web.dto.GroupResponse;
import com.ugoodtech.mdcc.web.dto.JsonResponse;
import com.ugoodtech.mdcc.web.service.QuestionService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnswerController {

    @Autowired
    private StationColorReportRepository stationColorReportRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    @Autowired
    private StationCustomerReportRepository stationCustomerReportRepository;

    @Autowired
    private StationRegularServiceReportRepository stationRegularServiceReportRepository;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private StationPaintReportRepository stationPaintReportRepository;

    private List<StationCustomerReport> customerReports = null;
    private List<StationRegularServiceReport> regularServiceReports = null;
    private List<StationColorReport> colorReports = null;
    private List<StationPaintReport> paintReports = null;

    @ApiOperation("获取章节分组和题目")
    @RequestMapping(value = "/getGroups", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getGroups(
            @ApiParam("章节id") @RequestParam(required = false) Long sectionId,
            @ApiParam("报告id") @RequestParam(required = false) Long reportId,
            @ApiParam("报告类型:colorReport,leanPaintReport,serviceReport,saleServiceReport,satisfactionReport") @RequestParam(required = false) String reportType
    ) {
        if (reportId != null && reportId != 0) {
            if (reportType.equals(Constants.CUSTOMER_REPORT)) {
                customerReports = stationCustomerReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.SERVICE_REPORT)) {
                regularServiceReports = stationRegularServiceReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.TECHCOLOR_REPORT)) {
                colorReports = stationColorReportRepository.selectAnswer(reportId);
            }
            if (reportType.equals(Constants.PAINT_REPORT)) {
                paintReports = stationPaintReportRepository.selectAnswer(reportId);
            }
        }
        List<QuestionGroup> questionGroups = questionService.getQuestions(sectionId);
        List<Object> dataList = new ArrayList<>();
        for (QuestionGroup questionGroup : questionGroups) {
            dataList.add(GroupResponse.turnGroupToResponse(questionGroup));
            List<Question> questionGroupList = questionService.getQuestionsByGroup(questionGroup.getId());
            for (Question question : questionGroupList) {

                if (reportId != null && reportId != 0) {
                    question = selectAnswer(question, reportId);
                }

                List<QuestionAnswerOption> questionAnswerOptions = questionService.getOptionsByGroup(question.getId());
                question.setOptions(questionAnswerOptions);
                List<Question> questions = new ArrayList<>();
                questions.add(question);
                if (QuestionCategory.TEXT.equals(question.getCategory())) {
                    Map<String, Object> textQuestion = new HashMap<>();
                    textQuestion.put("category", question.getCategory().name());
                    textQuestion.put("required", Boolean.valueOf(question.getRequired()));
                    textQuestion.put("type", "question");
                    textQuestion.put("inputList", questions);
                    dataList.add(textQuestion);
                } else {
                    dataList.add(question);
                }
            }
        }
        customerReports = null;
        regularServiceReports = null;
        colorReports = null;
        paintReports = null;
        return JsonResponse.successResponseWithData(dataList);
    }


    private Question selectAnswer(Question question, Long report_id) {
        if (customerReports != null) {
            for (StationCustomerReport report : customerReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }

        if (regularServiceReports != null) {
            for (StationRegularServiceReport report : regularServiceReports) {

                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }

                    if (report.getQuestionId() == 156L) {
                        QStationReport qStationReport = QStationReport.stationReport;
                        BooleanBuilder builder = new BooleanBuilder();
                        builder.and(qStationReport.deleted.eq(false));
                        builder.and(qStationReport.enabled.eq(true));
                        builder.and(qStationReport.id.eq(report_id));
                        StationReport stationReport = stationReportRepository.findOne(builder);
                        if (stationReport != null && StringUtils.isNotEmpty(stationReport.getUserIds())) {
                            question.setAnswer_other(stationReport.getUserIds());
                        }
                    }

                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }

        if (colorReports != null) {
            for (StationColorReport report : colorReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }

        if (paintReports != null) {
            for (StationPaintReport report : paintReports) {
                if (question.getId().equals(report.getQuestionId())) {
                    if (StringUtils.isNotEmpty(report.getAnswerOther())) {
                        question.setAnswer_other(report.getAnswerOther());
                    } else {
                        question.setAnswer_other("");
                    }
                    if (StringUtils.isNotEmpty(report.getAnswerPhoto())) {
                        question.setAnswer_photo(report.getAnswerPhoto());
                    } else {
                        question.setAnswer_photo("");
                    }
                    if (report.getAnswerId() != null && report.getAnswerId().indexOf(",") != -1) {
                        question.setSelected(report.getAnswerId());
                    } else if (report.getAnswerId() != null && Long.valueOf(report.getAnswerId()) != 0) {
                        question.setSelected(report.getAnswerId());
                    } else {
                        question.setSelected("");
                    }
                }
            }
            return question;
        }
        return question;
    }

}
